<?php

declare(strict_types=1);

namespace App\Kernel\Visitor;

use Hyperf\Database\Commands\Ast\ModelUpdateVisitor as Visitor;
use Hyperf\Stringable\Str;

class ModelUpdateVisitor extends Visitor
{
        /**
         * Used by `casts` attribute.
         */
        protected function formatDatabaseType(string $type): ?string
        {
                switch ($type) {
                        /*case 'tinyint':
                        case 'smallint':
                        case 'mediumint':
                        case 'int':
                                return 'integer';*/
                        case 'bigint':
                                return 'string';
                        /*case 'decimal':
                                // 设置为 decimal，并设置对应精度
                                //return 'float';
                        case 'float':
                        case 'double':
                        case 'real':
                                return 'float';*/
                        case 'bool':
                        case 'boolean':
                                return 'boolean';
                        default:
                                return null;
                }
        }

        /**
         * Used by `@property` docs.
         */
        protected function formatPropertyType(string $type, ?string $cast): ?string
        {
                if (!isset($cast)) {
                        $cast = $this->formatDatabaseType($type) ?? 'string';
                }
                switch ($cast) {
                        case 'integer':
                                return 'int';
                        case 'date':
                        case 'datetime':
                                return '\Carbon\Carbon';
                        case 'json':
                                return 'array';
                }
                if (Str::startsWith($cast, 'decimal')) {
                        // 如果 cast 为 decimal，则 @property 改为 string
                        return 'string';
                }
                return $cast;
        }
}
